package com.mp.ipm2020.mbg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.mp.ipm2020.mbg.dao.RoleDao;
import com.mp.ipm2020.mbg.mapper.RoleMenuRelationMapper;
import com.mp.ipm2020.mbg.mapper.RoleResourceRelationMapper;
import com.mp.ipm2020.mbg.model.*;
import com.mp.ipm2020.mbg.mapper.RoleMapper;
import com.mp.ipm2020.mbg.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author adidasshe
 * @since 2020-05-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private RoleResourceRelationMapper roleResourceRelationMapper;

    @Autowired
    private RoleDao roleDao;
//    @Autowired
//    private UserCacheService userCacheService;


    @Override
    public List<Role> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("name", keyword);
        }
        return roleMapper.selectList(wrapper);
    }

    @Override
    public List<Menu> getMenuList(Long userId) {
        System.out.println(roleDao.getMenuList(userId));
        return roleDao.getMenuList(userId);
    }

    @Override
    public List<Menu> listMenu(Long roleId) {
        return roleDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<Resource> listResource(Long roleId) {
        return roleDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        QueryWrapper<RoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.ge("role_id", roleId);
        roleMenuRelationMapper.delete(wrapper);
        //批量插入新关系
        for (Long menuId : menuIds) {
            RoleMenuRelation relation = new RoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        QueryWrapper<RoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleResourceRelationMapper.delete(wrapper);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            RoleResourceRelation relation = new RoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
//        userCacheService.delResourceListByRole(roleId);
        return resourceIds.size();
    }
}
